﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// TourismInfoList 的摘要说明
    /// </summary>
    public class TourismInfoList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            switch (action)
            {
                case "List":
                    int psize = int.Parse(context.Request["psize"]);
                    int pindex = int.Parse(context.Request["pindex"]);
                    string typeCode = context.Request["typeCode"];
                    string title = context.Request["title"];
                    string regionpath = context.Request["regionpath"];
                    string publish = context.Request["publish"];
                    int recordCount = 0;
                    List<PeekService.TourInfoModel> list = new PeekService.ExtendService().GetAllByTypeRegion(pindex, psize, out recordCount, typeCode, regionpath, "",title,publish);
                    int pageCount = 0;
                    if (recordCount % psize == 0)
                    {
                        pageCount = recordCount / psize;
                    }
                    else
                    {
                        pageCount = recordCount / psize + 1;
                    }
                    context.Response.Write(JsonConvert.SerializeObject(new { Data = new { Rows = list, Pager = new { PageIndex = pindex, Psize = psize, PageCount = pageCount } } }));
                    break;
                case "Order":
                    try
                    {
                        string code = context.Request["code"];
                        bool up = bool.Parse(context.Request["up"]);
                        new PeekService.ExtendService().SetOrder("TourismInfo", code, up);
                        context.Response.Write(JsonConvert.SerializeObject("1"));
                    }
                    catch
                    {
                        context.Response.Write("0");
                    }
                    break;
                case "Delete":
                    try
                    {

                        string code = context.Request["code"];
                        PeekService.TourismInfo model = new PeekService.TourismInfoService().GetModel(code);
                        if (model != null)
                        {
                            model.State = 1;
                            bool isTrue = new PeekService.TourismInfoService().Update(model);
                            if (isTrue)
                            {
                                context.Response.Write(JsonConvert.SerializeObject("1"));
                            }
                            else
                            {
                                context.Response.Write(JsonConvert.SerializeObject("0"));
                            }
                        }

                    }
                    catch
                    {
                        context.Response.Write(JsonConvert.SerializeObject("0"));
                    }
                    break;
                case "DeleteAll":
                    string ids = context.Request["ids"];
                    string tablename = context.Request["tablename"];
                    context.Response.Write(JsonConvert.SerializeObject(new PeekService.ExtendService().DeleteAll(ids.TrimEnd(','), tablename, false)));
                    break;
                case "Publish":
                    string pids = context.Request["ids"].TrimEnd(',');
                    bool pub = bool.Parse(context.Request["publish"]);
                    context.Response.Write(JsonConvert.SerializeObject(new PeekService.ExtendService().PublishTourInfo(pids, pub)));
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}